<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <link rel="stylesheet" th:href="@{/css/font.css}"/>
    <link rel="stylesheet" th:href="@{/css/xadmin.css}"/>
    <link rel="stylesheet" th:href="@{/lib/layui/css/modules/eleTree/eleTree.css}"/>

    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <script type="text/javascript" th:src="@{/lib/layui/lay/modules/eleTree.js}"></script>

    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        html,body{
            width: 80%;
            margin: 0 auto;
            background-color: #fff;
            padding: 15px 0px 30px 0px;
            scroll-behavior: smooth;
        }
        .eleTree{
            width: 500px;
            border: 1px solid #ccc;
            overflow: hidden;
        }
        a:hover{
            color: #01AAED;
        }
        .nav ul{
            padding:10px 0px 10px 35px;
        }
        .nav ul li{
            padding: 5px 0px;
            list-style-type: decimal;
        }
        .nav ul a{
            color: #333;
            font-size: 14px;
            text-decoration: none;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <input type="hidden" name="id" th:value="${id}" />
        <div class="eleTree ele1" lay-filter="data1" th:name="menuIdArray"></div>
        <div class="layui-form-item" style="margin-top: 30px;">
            <button  class="layui-btn" onclick="menusConfig()">保存</button>
        </div>
    </div>
</div>
<script th:inline="javascript">
    var checkedArray;
    layui.use(['jquery','table','eleTree','code'], function(){
        var $ = layui.jquery;
        var eleTree = layui.eleTree;
        var table = layui.table;
        var code = layui.code;

        var data= [[${menus}]];
        var selectedMenuIds = [[${menuIds}]];

        console.log('start')
        console.log(data)
        console.log('end')
        console.log(selectedMenuIds)


        var el = eleTree.render({
            elem: '.ele1',
            showLine:true,
            indent:64,
            defaultExpandAll:true,
            data: data,
            showCheckbox: true
        });
        el.setChecked(selectedMenuIds);
        checkedArray = el.getChecked(false, true);
        eleTree.on("nodeChecked(data1)",function(d) {
            checkedArray = el.getChecked(false, true);
        })
    });
    function menusConfig() {
        var id = [[${id}]];
        var ids='';

        for (var i = 0; i < checkedArray.length; i++) {
            if (i==0){
                ids = ids + checkedArray[i].id;
            } else {
                ids = ids + '_' + checkedArray[i].id;
            }
        }
        console.log(ids)
        $.ajax({
            type: 'post',
            data: {nodes:ids,id:id},
            url: '/sys/role/menu/update',
            cache:false,
            dataType:'json',
            success: function (data) {
                if(parseInt(data)==1){
                    layer.alert("保存成功", {
                        icon: 6
                    }, function() {
                        //关闭当前frame
                        xadmin.close();
                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
                }else{
                    layer.alert(data);
                }
            },error:function () {
                layer.alert('保存失败');
            }
        });


    }
</script>
</body>

</html>
